package bishi2016;

//按照主对角线方向从右上角一直打印至左下角
//[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],4
//[4,3,8,2,7,12,1,6,11,16,5,10,15,9,14,13]即斜着打，主对角线方向
public class Meituan_PrintErArray {

	public static void main(String[] args) {
		int[] dd = new int[15];
		int[] ss;
		final int[] result = dd;
		result[5] = 1;
		ss = arrayPrint(new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}, 4);
		System.out.println(" -----");
		for (int i = 0; i < ss.length; i++) {
			System.out.println(ss[i]);
		}
	}
	public static int[] arrayPrint(int[][] arr, int n) {
		//初始打印1个数字，之后逐渐累加,到n之后再递减
		int i = 0; 
		int j = n - 1;
		int[] res = new int[n * n];
		int index = 0;
		int count = 0;
		int shouldCount = 1;
		int tempI, tempJ = 0;
		boolean flag = true;
		//如果没有到达左下角
		while(i != n - 1 || j != 0){
			count = 0;
			tempI = i;
			tempJ = j;
			while(count < shouldCount){
				System.out.println("i : " + tempI + " j: " + tempJ + " arr:  " + arr[tempI][tempJ]);
				res[index++] = arr[tempI][tempJ];
				count++;
				if(count == shouldCount){
					break;
				}
				if(tempI < n - 1){
					tempI = tempI + 1;
				}
				if(tempJ < n - 1){
					tempJ = tempJ + 1;
				}
			}
			if(shouldCount < n && flag){
				shouldCount++;
				i = 0;
				j--;
				System.out.println("shouldCount:" + shouldCount + "i: " + i +"j: " + j);
			}else{
				flag = false;
				shouldCount--;
				System.out.println("shouldCount11:" + shouldCount);
				i++;
				j = 0;
			}
			//上面的判断其实可以写成下面这样
			if(j > 0 ){
				shouldCount++;
				j--;
				System.out.println("shouldCount:" + shouldCount + "i: " + i +"j: " + j);
			}else{
				shouldCount--;
				System.out.println("shouldCount11:" + shouldCount);
				i++;
			}
			res[index] = arr[n - 1][0];
		}
		return res;
    }
}
